﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Lamex.Library;
using Lamex.Interface;
using Lamex.Common;
using Lamex.Library.CommonClass;
using DevExpress.XtraGrid.Views.Grid;
using System.IO;
using Lamex.Base;
using System.Threading;
using System.Collections;

namespace Lamex.mPPC.UI
{
    public partial class frmPPC_HPL430 : frmBaseChild
    {
        //所选订单生成CSV文件，保存路径：\\Tx-fs10\tx-fs1\LAMEX\PPC\HPL430\CSV\toConvert
        private static string toConvert_path = @"\\Tx-fs10\tx-fs1\LAMEX\PPC\HPL430\CSV\toConvert\";
        private static string Converted_path = @"\\Tx-fs10\tx-fs1\LAMEX\PPC\HPL430\CSV\Converted\";
        private static string toTransfer_path = @"\\Tx-fs10\tx-fs1\LAMEX\PPC\HPL430\PTX\toTransfer\";
        private static string Transfered_path = @"\\Tx-fs10\tx-fs1\LAMEX\PPC\HPL430\PTX\Transfered\";
        private static string HPO_path = @"\\Tx-fs10\tx-fs1\LAMEX\PPC\HPL430\Test\";
        private static string DBPlatten_path = @"\\tx-fs10\TX-FS1\LAMEX\PPC\HPL430\Lamex_PTX\SchellingData\DBPlatten\";
        private static string Optimization_No_PTX = "";//PTX-双击获取的优化单号
        public frmPPC_HPL430()
        {
            InitializeComponent();
        }

        private void frmPPC_HPL430_Resize(object sender, EventArgs e)
        {
            splitContainerControl1.SplitterPosition = (this.Height - 66) / 2;

            splitContainerControl2.SplitterPosition = this.Width / 5;
            splitContainerControl3.SplitterPosition = this.Width / 5;
            splitContainerControl4.SplitterPosition = this.Width / 5;
            splitContainerControl5.SplitterPosition = this.Width / 5;
        }

        private void frmPPC_HPL430_Load(object sender, EventArgs e)
        {
            try { base.InitButtons(); } catch { }//初始化标准功能，因要应用于RibbonForm，所以加入异常处理
            //设置画面最大开料尺寸
            txtSize.Text = "60";
            Refresh_Cutting_State();
        }

        #region 右键操作

        private void menuItemCSV_File_Generate_Click_1(object sender, EventArgs e)
        {
            do_CSV_File_Generate();
        }

        private void menuItemPTX_Transfer_To_HPL430_Click(object sender, EventArgs e)
        {
            do_PTX_Transfer_To_HPL430();
        }

        private void menuItemCutting_State_Refresh_Click(object sender, EventArgs e)
        {
            Refresh_Cutting_State();
        }
        private void menuItemPTX_Cancel_HPL430_Click(object sender, EventArgs e)
        {
            Cancel_PTX_WaitFile();
        }
        private void contextMenuStrip2_Opening(object sender, CancelEventArgs e)
        {
            string whichcontrol_name = (sender as ContextMenuStrip).SourceControl.Name;
            switch (whichcontrol_name)
            {
                case "gridControl1":
                    menuItemCSV_File_Generate.Visible = true;
                    menuItemCutting_State_Refresh.Visible = true;
                    menuItemPTX_Transfer_To_HPL430.Visible = false;
                    menuItemPTX_Cancel_HPL430.Visible = false;
                    break;
                case "gridControl2":
                    menuItemCSV_File_Generate.Visible = false;
                    menuItemPTX_Transfer_To_HPL430.Visible = false;
                    menuItemPTX_Cancel_HPL430.Visible = true;
                    menuItemCutting_State_Refresh.Visible = true;
                    break;
                case "gridControl3":
                    menuItemCSV_File_Generate.Visible = false;
                    menuItemPTX_Transfer_To_HPL430.Visible = true;
                    menuItemCutting_State_Refresh.Visible = true;
                    menuItemPTX_Cancel_HPL430.Visible = false;
                    break;
                default:
                    menuItemCSV_File_Generate.Visible = false;
                    menuItemPTX_Transfer_To_HPL430.Visible = false;
                    menuItemPTX_Cancel_HPL430.Visible = false;
                    menuItemCutting_State_Refresh.Visible = true;
                    break;
            }
        }
        #endregion

        #region 按钮操作

        //生成CSV文件
        private void sbMES_To_CSV_Click(object sender, EventArgs e)
        {
            do_CSV_File_Generate();
        }

        //将PTX传到HPL430
        private void sbPTX_Transfer_To_HPL430_Click(object sender, EventArgs e)
        {
            do_PTX_Transfer_To_HPL430();
        }

        //刷新开料进度
        private void sbCutting_State_Refresh_Click(object sender, EventArgs e)
        {
            Refresh_Cutting_State();
        }
        //待转csv双击行显示明细
        private void gridView1_MouseDown(object sender, MouseEventArgs e)
        {
            DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo hInfo = gridView1.CalcHitInfo(new Point(e.X, e.Y));
            if (e.Button == MouseButtons.Left && e.Clicks == 2)
            {
                //判断光标是否在行范围内 
                if (hInfo.InRow)
                {
                    try
                    {
                        SplashScreenTool.ShowSplashScreen(typeof(WaitForm1));
                        gridView0.Columns.Clear();
                        this.删除ToolStripMenuItem.Visible = false;
                        this.重生成CSVToolStripMenuItem.Visible = false;
                        gridView0.OptionsSelection.MultiSelect = false;
                        gridView0.OptionsSelection.MultiSelectMode = DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect;
                        string strORDER_NO = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "PRODUCT_ORDER_NO").ToString();
                        string strFollw_NO = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "FOLLOW_NO").ToString();
                        gridControl0.DataSource = Common.GetDataTable.GetTable_Detail(" ISNULL(doneCSV,'')='' and a.PRODUCT_ORDER_NO='" + strORDER_NO + "' and a.FOLLOW_NO='" + strFollw_NO + "' ");
                        setGridViewAutoFitColumns(gridView0);
                        SplashScreenTool.CloseSplashScreen();
                    }
                    catch (Exception ex)
                    {
                        SplashScreenTool.CloseSplashScreen();
                    }

                }
            }
        }
        //待转ptx双击行显示明细
        private void gridView2_MouseDown(object sender, MouseEventArgs e)
        {
            DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo hInfo = gridView2.CalcHitInfo(new Point(e.X, e.Y));
            if (e.Button == MouseButtons.Left && e.Clicks == 2)
            {
                //判断光标是否在行范围内 
                if (hInfo.InRow)
                {
                    try
                    {

                        SplashScreenTool.ShowSplashScreen(typeof(WaitForm1));
                        gridView0.Columns.Clear();
                        this.删除ToolStripMenuItem.Visible = true;
                        this.重生成CSVToolStripMenuItem.Visible = true;
                        //显示勾选框
                        gridView0.OptionsSelection.MultiSelect = true;
                        gridView0.OptionsSelection.MultiSelectMode = DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.CheckBoxRowSelect;
                        Optimization_No_PTX = gridView2.GetRowCellValue(gridView2.FocusedRowHandle, "优化单号").ToString();
                        //Task.Run(() =>)
                        gridControl0.DataSource = Common.GetDataTable.GetTable_Detail_PTX(" INFO_20='" + Optimization_No_PTX + "' AND ISNULL(DELETE_FLAG,'')='' ");
                        setGridViewAutoFitColumns(gridView0);
                        SplashScreenTool.CloseSplashScreen();
                    }
                    catch (Exception ex)
                    {
                        SplashScreenTool.CloseSplashScreen();
                    }

                }
            }
        }
        //待转HP430显示明细
        private void gridView3_MouseDown(object sender, MouseEventArgs e)
        {
            DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo hInfo = gridView3.CalcHitInfo(new Point(e.X, e.Y));
            if (e.Button == MouseButtons.Left && e.Clicks == 2)
            {
                //判断光标是否在行范围内 
                if (hInfo.InRow)
                {
                    try
                    {
                        SplashScreenTool.ShowSplashScreen(typeof(WaitForm1));
                        gridView0.Columns.Clear();
                        this.删除ToolStripMenuItem.Visible = false;
                        this.重生成CSVToolStripMenuItem.Visible = false;
                        //显示勾选框
                        gridView0.OptionsSelection.MultiSelect = false;
                        gridView0.OptionsSelection.MultiSelectMode = DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect;
                        Optimization_No_PTX = gridView3.GetRowCellValue(gridView3.FocusedRowHandle, "优化单号").ToString();
                        gridControl0.DataSource = Common.GetDataTable.GetTable_Detail_PTX(" INFO_20='" + Optimization_No_PTX + "' AND ISNULL(DELETE_FLAG,'')='' ");
                        setGridViewAutoFitColumns(gridView0);
                        SplashScreenTool.CloseSplashScreen();
                    }
                    catch (Exception ex)
                    {
                        SplashScreenTool.CloseSplashScreen();
                    }

                }
            }
        }
        #endregion

        #region 通用功能执行模块

        //生成CSV文件
        private void do_CSV_File_Generate()
        {
            //判断【待出CSV】是否选择订单
            //To Do .......
            //将所选订单生成CSV文件，保存路径：\\Tx-fs10\tx-fs1\LAMEX\PPC\HPL430\CSV\toConvert
            //逻辑由Steve提供
            //To Do .......
            //在【PPC_HPL430_Cutting_State】表【doneCSV】列中写入CSV生成时间
            //To Do .......
            try
            {
                if (string.IsNullOrEmpty(txtSize.Text))
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("最大开料尺寸不允许为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                List<string> strList = new List<string>();//保存勾选的生产订单号 
                List<string> strList1 = new List<string>();//保存勾选的跟进表号 
                List<string> list_Error = new List<string>();
                DirectoryInfo root_DBP = new DirectoryInfo(DBPlatten_path);
                DataTable dataTable1 = Common.GetTable.GetTable_FollwNo();
                int[] iRow_Routing = gridView1.GetSelectedRows();
                if (iRow_Routing.Length <= 0)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("请选择需要转换的订单！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                foreach (int item in iRow_Routing)
                {
                    DataRow dr = dataTable1.NewRow();
                    string strORDERNO = Convert.ToString(gridView1.GetRowCellValue(item, "PRODUCT_ORDER_NO"));
                    string strFOLLOWNO = Convert.ToString(gridView1.GetRowCellValue(item, "FOLLOW_NO"));
                    strList.Add(strORDERNO);
                    strList1.Add(strFOLLOWNO);
                    dr["PRODUCT_ORDER_NO"] = strORDERNO;
                    dr["FOLLOW_NO"] = strFOLLOWNO;
                    dataTable1.Rows.Add(dr);
                }
                //增加判断\\tx-fs10\TX-FS1\LAMEX\PPC\HPL430\Lamex_PTX\SchellingData\DBPlatten\路径是否存在所选的decor
                DataTable table = Common.GetDataTable.GetMES_Decor(strList, strList1);
                if (table.Rows.Count > 0)
                {

                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        int k = 0;
                        foreach (FileInfo f in root_DBP.GetFiles("*.lck"))
                        {
                            if (f.Name.Contains(table.Rows[i]["MES_DECOR"].ToString()))
                            {
                                k++;
                                break;
                            }
                        }
                        if (k == 0)
                            list_Error.Add(table.Rows[i]["MES_DECOR"].ToString());
                    }
                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        int k = 0;
                        foreach (FileInfo f in root_DBP.GetFiles("*.pdb"))
                        {
                            if (f.Name.Contains(table.Rows[i]["MES_DECOR"].ToString()))
                            {
                                k++;
                                break;
                            }
                        }
                        if (k == 0)
                            list_Error.Add(table.Rows[i]["MES_DECOR"].ToString());
                    }
                }
                if (list_Error.Count > 0)
                {
                    string error = string.Join("\r\n", list_Error.Distinct().ToArray());
                    DevExpress.XtraEditors.XtraMessageBox.Show("以下号码尚未在HPO新建板材：请新建板材后再重新生成CSV\r\n" + error, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                SplashScreenTool.ShowSplashScreen(typeof(WaitForm1));
                //生成CSV，并保存
                string filename = "O_" + DateTime.Now.ToString("yyMMdd") + "_" + DateTime.Now.ToString("HHmmss") + "_HPL";
                string path = toConvert_path + filename + ".csv";
                DataTable dataTable = Common.LogicHelper.DataImport_CSV(strList, strList1, filename, txtSize.Text);//csv数据表
                DataTable dt_CuttingState = Common.LogicHelper.Data_CuttingState(dataTable1, filename);//文件状态表
                //插入数据表【PPC_HPL430_CSV】             
                if (Lamex.Base.BaseBuildSql.ExcuteInsert(dataTable, "PPC_HPL430_CSV"))
                // if (Lamex.mPPC.Common.BaseBuildSql.ExcuteInsert(dataTable, "PPC_HPL430_CSV"))
                {
                    //新增记录到【PPC_HPL430_Cutting_State】表
                    if (Lamex.Base.BaseBuildSql.ExcuteInsert(dt_CuttingState, "PPC_HPL430_Cutting_State"))
                    {
                        //导出CSV
                        //去掉写入时间和写入人，统一csv导出格式
                        dataTable.Columns.Remove("UPDATE_TS");//写入时间
                        dataTable.Columns.Remove("UPDATE_USER");//写入人
                        Common.CSVHelper.DataTableToCSV(dataTable, path);
                    }
                    //刷新开料进度
                    Refresh_Cutting_State();
                }

                SplashScreenTool.CloseSplashScreen();
            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                SplashScreenTool.CloseSplashScreen();
            }
        }

        //将PTX传到HPL430
        private void do_PTX_Transfer_To_HPL430()
        {
            //判断【待传HPL430】是否选择优化单
            //To Do .......

            //遍历\\Tx-fs10\tx-fs1\LAMEX\PPC\HPL430\PTX\toTransfer路径下PTX文件  
            //将...\CSV\toConvert下同名CSV文件移动到...\CSV\Converted
            //将...\PTX\toTransfer路径下PTX文件移动到...\PTX\Transfered
            //将...\PTX\toTransfer路径下PTX文件复制到HPL430任务清单路径下?
            //在【PPC_HPL430_Cutting_State】表【doneTransfer】列中写入转HPL430时间
            //To Do .......

            try
            {
                int[] iRow_Routing = gridView3.GetSelectedRows();
                if (iRow_Routing.Length <= 0)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("请选择需要传输的优化单！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                DirectoryInfo root_CSV = new DirectoryInfo(toConvert_path);
                DirectoryInfo root_PTX = new DirectoryInfo(toTransfer_path);
                List<string> list_Error = new List<string>();
                List<string> error_thick = new List<string>();
                //增加检测ptx文件逻辑
                foreach (int item in iRow_Routing)
                {
                    string optionName = Convert.ToString(gridView3.GetRowCellValue(item, "优化单号"));
                    foreach (FileInfo f in root_PTX.GetFiles())
                    {
                        string strthick_m = "";//MATERIALS的厚度
                        string strthick_p = "";//PARTS_UDI的厚度
                        int i = 0;//是否第一次检查厚度
                        string strBarCode = "";//第一个条码(流水号)

                        if (f.Name.Replace(".ptx", "") == optionName)
                        {
                            //采用流的方式读取
                            StreamReader sr = new StreamReader(f.FullName, Encoding.Default);
                            string nextLine;
                            while ((nextLine = sr.ReadLine()) != null)
                            {
                                string strList = nextLine.ToString();
                                string[] str = strList.Split(',');
                                if (str[0] == "MATERIALS")
                                {
                                    if (Convert.ToDouble(str[9]) <= 0 || Convert.ToDouble(str[10]) <= 0 || Convert.ToDouble(str[11]) <= 0 || Convert.ToDouble(str[12]) <= 0)
                                    {
                                        list_Error.Add(str[3]);                                       
                                    }
                                    strthick_m = str[5];
                                }
                                //检查厚度
                                if (str[0] == "PARTS_UDI" && i == 0)
                                {
                                    strBarCode = str[23];
                                    DataTable dataTable = Common.GetDataTable.GetThick(strBarCode);
                                    if (dataTable.Rows.Count > 0)
                                        strthick_p = dataTable.Rows[0]["THICKNESS"].ToString();
                                    i++;
                                }
                            }
                            //检查厚度
                            if (strthick_m.Trim('0')!= strthick_p.Trim('0'))
                                error_thick.Add(optionName);
                            
                            sr.Close();
                            sr.Dispose();
                        }
                    }
                }
                if (list_Error.Count > 0)
                {
                    string error = string.Join("\r\n", list_Error.Distinct().ToArray());
                    DevExpress.XtraEditors.XtraMessageBox.Show("以下号码未设置修边值：\r\n" + error, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                if(error_thick.Count>0)
                {
                    string error = string.Join("\r\n", error_thick.Distinct().ToArray());
                    DevExpress.XtraEditors.XtraMessageBox.Show("板材厚度设置有误：\r\n" + error, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                SplashScreenTool.ShowSplashScreen(typeof(WaitForm1));
                foreach (int item in iRow_Routing)
                {
                    string optionName = Convert.ToString(gridView3.GetRowCellValue(item, "优化单号"));
                  
                    //将...\CSV\toConvert下同名CSV文件移动到...\CSV\Converted
                    foreach (FileInfo f in root_CSV.GetFiles())
                    {
                        if (f.Name.Replace(".csv", "") == optionName)
                        {
                            //File.Copy(f.FullName, Converted_path + f.Name, true);
                            if (!File.Exists(Converted_path + f.Name))
                                File.Move(f.FullName, Converted_path + f.Name);
                        }
                    }
                 
                    //将...\PTX\toTransfer路径下PTX文件移动到...\PTX\Transfered
                    foreach (FileInfo f in root_PTX.GetFiles())
                    {
                        if (f.Name.Replace(".ptx", "") == optionName)
                        {
                            //将...\PTX\toTransfer路径下PTX文件复制到HPL430任务清单路径下?
                            File.Copy(f.FullName, HPO_path + f.Name, true);

                            if (!File.Exists(Transfered_path + f.Name))
                                File.Move(f.FullName, Transfered_path + f.Name);
                        }
                    }

                    //在【PPC_HPL430_Cutting_State】表【doneTransfer】列中写入转HPL430时间
                    Common.LogicHelper.Update_Cutting_State_Tranfer(optionName, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));

                }
                //刷新开料进度
                Refresh_Cutting_State();
                SplashScreenTool.CloseSplashScreen();
            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                SplashScreenTool.CloseSplashScreen();
            }
        }

        //取消待转PTX文件
        private void Cancel_PTX_WaitFile()
        {
            try
            {
                int[] iRow_Routing = gridView2.GetSelectedRows();
                if (iRow_Routing.Length <= 0)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("请选择需要取消的优化单！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                SplashScreenTool.ShowSplashScreen(typeof(WaitForm1));
                DirectoryInfo root_CSV = new DirectoryInfo(toConvert_path);
                Dictionary<string, string> keyValues = new Dictionary<string, string>();
                StringBuilder stringBuilder = new StringBuilder();//存放HPO已读取的文件名
                foreach (int item in iRow_Routing)
                {
                    string optionName = Convert.ToString(gridView2.GetRowCellValue(item, "优化单号"));
                    string state = Convert.ToString(gridView2.GetRowCellValue(item, "State"));
                    keyValues.Add(optionName, state);

                    ////删除表【PPC_HPL430_Cutting_State】的记录
                    //Common.CanelHelper.Delete_Cutting_State_Cancel(optionName);
                    ////删除表【PPC_HPL430_CSV】的记录
                    //Common.CanelHelper.Delete_CSV_OPT(optionName);
                    ////移除已生成的csv文件
                    //foreach (FileInfo f in root_CSV.GetFiles())
                    //{
                    //    if (f.Name.Replace(".csv", "") == optionName)
                    //    {
                    //        if (File.Exists(f.FullName))
                    //            File.Delete(f.FullName);
                    //    }
                    //}
                    ////移除HPO已读入的csv文件

                }
                foreach (var item in keyValues)
                {
                    if (item.Value.Contains("HPO已读入"))
                    {
                        stringBuilder.Append(item.Key + "\r\n");
                    }
                }
                //移除HPO已读入的csv文件
                if (stringBuilder.Length > 0)
                {
                    DialogResult dr = DevExpress.XtraEditors.XtraMessageBox.Show("以下单号已导入，是否继续？\r\n如要继续，请将HPO内对应单号删除！\r\n" + stringBuilder.ToString(), "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                    if (dr == DialogResult.OK)
                    {
                        Common.CanelHelper.Delete_PTX_HPO(root_CSV, keyValues);
                    }
                }
                //移除正常生成的csv文件
                else
                {
                    Common.CanelHelper.Delete_PTX_NOHPO(root_CSV, keyValues);
                }
                //刷新开料进度
                Refresh_Cutting_State();

                SplashScreenTool.CloseSplashScreen();
            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                SplashScreenTool.CloseSplashScreen();
            }
        }
        //刷新开料进度
        private void Refresh_Cutting_State()
        {
            Refresh_MES_To_CSV_Data();
            Refresh_CSV_To_PTX_Data();
            Refresh_PTX_To_HPL430_Data();
            Refresh_Cutting_Data();
            Refresh_Cutted_Data();
            setGridViewAutoFitColumns(gridView1);
            setGridViewAutoFitColumns(gridView2);
            setGridViewAutoFitColumns(gridView3);
            setGridViewAutoFitColumns(gridView4);
            setGridViewAutoFitColumns(gridView5);
            //显示页脚页码
            DevStyle.SetGridControlLayout(gridControl0, false);
            //清空明细
            gridControl0.DataSource = new DataTable();
        }

        #endregion

        #region 窗体数据刷新

        //刷新【待出CSV】
        private void Refresh_MES_To_CSV_Data()
        {
            //刷新【待出CSV】，即所有未出CSV订单           
            gridControl1.DataSource = Common.GetDataTable.GetTable_CSV();
        }

        //刷新【待转PTX】数据
        private void Refresh_CSV_To_PTX_Data()
        {
            //刷新\\Tx-fs10\tx-fs1\LAMEX\PPC\HPL430\CSV\toDo优化单数据到【待转PTX】
            //To Do ......
            try
            {
                DirectoryInfo root = new DirectoryInfo(toConvert_path);
                List<string> list = new List<string>();
                foreach (FileInfo f in root.GetFiles())
                {
                    if (f.Name.Contains("csv"))
                        list.Add(f.Name.Replace(".csv", ""));
                }
                gridControl2.DataSource = Common.LogicHelper.GetGridData_PTX(list, root);
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.ToString());
            }


        }

        //刷新【待传HPL430】数据
        private void Refresh_PTX_To_HPL430_Data()
        {
            //刷新\\Tx-fs10\tx-fs1\LAMEX\PPC\HPL430\PTX\toDo优化单数据到【待传HPL430】
            //To Do ......

            //将...\PTX\toDo中的PTX文件创建时间写入【PPC_HPL430_Cutting_State】表【donePTX】列中
            try
            {
                DirectoryInfo root = new DirectoryInfo(toTransfer_path);
                List<string> list = new List<string>();
                foreach (FileInfo f in root.GetFiles())
                {
                    if (f.Name.Contains("ptx"))
                    {
                        FileInfo fileInfo = new FileInfo(f.FullName);
                        string optionName = f.Name.Replace(".ptx", "");
                        string CreationTime = fileInfo.CreationTime.ToString();
                        list.Add(optionName);
                        Common.LogicHelper.Update_Cutting_State_PTX(optionName, CreationTime);
                    }
                }
                gridControl3.DataSource = Common.LogicHelper.GetOptionName(list);

            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.ToString());
            }

        }

        //刷新【开料中】数据
        private void Refresh_Cutting_Data()
        {
            //逻辑由Steve提供
            //To Do ......
        }

        //刷新【已开料】数据
        private void Refresh_Cutted_Data()
        {
            //逻辑由Steve提供
            //To Do ......
        }

        #endregion

        #region 选项区显示/隐藏、表复制设置、表页脚设置、基础配置窗体跳转

        private void 隐藏选项区ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            panelTop.Height = 0;
        }

        private void 显示选项区ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            panelTop.Height = 60;
        }
        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (gridView0.Columns.Count > 0)
                {
                    if (!gridView0.Columns.Contains(gridView0.Columns["BARCODE"]))
                    {
                        DevExpress.XtraEditors.XtraMessageBox.Show("非待转PTX文件，不允许删除操作！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    List<string> strList = new List<string>();//保存勾选的跟进表号              
                    int[] iRow_Routing = gridView0.GetSelectedRows();
                    if (iRow_Routing.Length <= 0)
                    {
                        DevExpress.XtraEditors.XtraMessageBox.Show("请选择需要删除的记录！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    SplashScreenTool.ShowSplashScreen(typeof(WaitForm1));
                    foreach (int item in iRow_Routing)
                    {
                        string strBarCode = Convert.ToString(gridView0.GetRowCellValue(item, "BARCODE"));
                        strList.Add(strBarCode);

                    }
                    if (Common.CanelHelper.Update_CSV(strList))
                    {
                        DevExpress.XtraEditors.XtraMessageBox.Show("删除成功！下一步请点击重生成CSV", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        gridControl0.DataSource = Common.GetDataTable.GetTable_Detail_PTX(" INFO_20='" + Optimization_No_PTX + "' AND ISNULL(DELETE_FLAG,'')='' ");
                    }
                    SplashScreenTool.CloseSplashScreen();
                }
            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                SplashScreenTool.CloseSplashScreen();
            }

        }

        private void 重生成CSVToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (gridView0.Columns.Count > 0)
                {
                    if (!gridView0.Columns.Contains(gridView0.Columns["BARCODE"]))
                    {
                        DevExpress.XtraEditors.XtraMessageBox.Show("非待转PTX文件，不允许该操作！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    SplashScreenTool.ShowSplashScreen(typeof(WaitForm1));
                    DataTable dataTable = Common.GetDataTable.GetTable_Detail_PTX_EN(" INFO_20='" + Optimization_No_PTX + "' AND ISNULL(DELETE_FLAG,'')='' ");
                    string path = toConvert_path + Optimization_No_PTX + ".csv";
                    Common.CSVHelper.DataTableToCSV(dataTable, path);
                    DevExpress.XtraEditors.XtraMessageBox.Show("生成CSV文件成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    SplashScreenTool.CloseSplashScreen();
                }
            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                SplashScreenTool.CloseSplashScreen();
            }
        }
        //gridView仅复制单元格内容，复制选中行焦点单元格的值
        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control & e.KeyCode == Keys.C)
            {
                Clipboard.SetDataObject(gridView1.GetFocusedRowCellValue(gridView1.FocusedColumn));
                e.Handled = true;
            }

            //或设置属性
            //optionBehavior - CopyToClipboardWithColumnHeader = false;
            //optionSelection - MultiSelectMode = CellSelect;
        }

        //表页脚设置
        private void TotalSum()
        {
            gridView1.Columns[0].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Count;
            gridView1.Columns[0].SummaryItem.DisplayFormat = "合计：";
            if (gridView1.Columns.Contains(gridView1.Columns[1]))
            {
                gridView1.Columns[1].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;
                this.gridView1.Columns[1].SummaryItem.DisplayFormat = "{0:N}";
                // this.gridView1.Columns[1].Width = 80;
            }
            if (gridView1.Columns.Contains(gridView1.Columns[2]))
            {
                gridView1.Columns[2].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;
                this.gridView1.Columns[2].SummaryItem.DisplayFormat = "{0:N}";
                //this.gridView1.Columns[2].Width = 80;
            }
        }

        //基础配置窗体跳转
        private void labelGoto_Click(object sender, EventArgs e)
        {
            Loginer.CurrentUser.FunctionId = "menuPPC_HPL430_Panel_Cutting_List";
            MdiTools.OpenChildWebBrowse(this.MdiParent as IMdiForm, new frmBaseChildWinForm("Transfer_Panel_Cutting", 1, false, false), sender as ToolStripMenuItem);
        }

        #endregion

        #region 实例化标准功能

        public override void DoView(IButtonInfo sender)
        {
            sbCutting_State_Refresh_Click(null, null);
            base.DoView(sender);
        }

        #endregion

        #region 自动列宽

        private void setGridViewAutoFitColumns(GridView gv)
        {
            gv.BestFitColumns();
            gv.OptionsView.ColumnAutoWidth = false;
        }

        #endregion

        private void labelHelp_HPO_Config_Click(object sender, EventArgs e)
        {
            //跳转到帮助文件
            //帮助文件由Steve提供
            //Help.ShowHelp(this, @"\\Tx-fs10\tx-fs1\LAMEX\MES\AppMgmt\Help\IMAWOP安装说明.chm");
            Help.ShowHelp(this, @"\\Tx-fs10\tx-fs1\LAMEX\MES\AppMgmt\Help\HPO转PTX使用说明.chm");
        }
        
    }
}